<?php
require_once "modules/config.class.php";
require_once "modules/debug.class.php";
require_once "modules/i18n.class.php";
require_once "modules/themes.class.php";
require_once "modules/persistence/connection.class.php";
require_once "modules/persistence/persistence.class.php";
require_once "modules/model/model.class.php";
require_once "modules/model/xmldataloader.class.php";

/**
 * <h2>Application manager class</h2>.
 * <p>The <code>Application</code> object is the main object of the framework.
 * It's the object instanciated to generate page and manage navigation, data acces, etc...</p>
 * <p>The initialisation of this object instanciate all sub managers as Config, Persistence, Model, XmlDataLoader,</p>
 * <p>To display the main page, just call url to index.php. </p>
 * @author Frederic Delorme<frederic.delorme@gmail.com>
 * @version 1.0 - 20091212
 */
class Application{
	/**
	 * Instance for Application Singleton.
	 * @var unknown_type
	 */
	static private $_instance;
	/**
	 * Configuration instance  singleton.
	 * @var Config
	 */
	private $config = null;
	/**
	 * Page Cache manager  singleton.
	 * @var Cache
	 */
	private $cache = null;
	/**
	 * Themes manager singleton
	 * @var Themes
	 */
	private $themes = null;
	/**
	 * Multi-Language manager instance.
	 * @var I18n
	 */
	private $language=null;
	/**
	 * Model Manager singleton instance.
	 * @var Model
	 */
	private $model = null;
	/**
	 * Persistence Manager instance.
	 * @var Persistence
	 */
	private $persistence = null;
	/**
	 * Current version for the framework.
	 * @var unknown_type
	 */
	private $version="0.1-beta";
	
	/**
	 * Time generation mesurement.
	 */	
	private $startTime=null;
	private $endTime=null;

	/**
	 * default constructor.
	 * Initialise all neede components to display main page.
	 */
	function __construct(){
		$this->init();	
	}
	/**
	 * Initialisation of the application
	 * @return unknown_type
	 */
	protected function init(){
		session_start();
		$this->config = Config::getInstance();
		__info(__FILE__,__LINE__,"*** build page :".$_SERVER['SCRIPT_FILENAME']." / ".$_SERVER['QUERY_STRING']);		
		if($this->config->get("cache","activate")){
			$this->cache = Cache::getInstance();
		}else{
			__info(__FILE__,__LINE__,"no cache");		
		}
		if($this->config->isExists("database")){
			$conDescriptor=$this->config->getGroup('database');
			__info(__FILE__,__LINE__,"connect to ".$conDescriptor['servername'].":".$conDescriptor['portnumber']);
			$this->database = Connection::getDatabase($conDescriptor);
			$this->database->open($conDescriptor);
			$this->persistence=Persistence::getInstance($this->database);
		}else{
			__info(__FILE__,__LINE__,"no database");		
		}
		$this->language = I18n::getInstance();
		$this->themes = Themes::getInstance($this->cache);
		
	}
	/**
	 * Pare and analyse HTTP query string to manage needed action.
	 * @return boolean: if any action engaged, return true.
	 */
	protected function parseQuery(){
		__debug(__FILE__,__LINE__,"Application::parseQuery() - start");
		// generate structure if generate params = true.
		if(isset($_REQUEST['generate'])){
			__debug(__FILE__,__LINE__,"Application::parseQuery() - generate model");
			$this->model = Model::getInstance($this->database);
			$this->model->generate(isset($_REQUEST['forced']));
		}
		if(isset($_REQUEST['reload'])){
			__debug(__FILE__,__LINE__,"Application::parseQuery() - reload data");
			$this->model = Model::getInstance($this->database);
			$xmlDataLoader = XmlDataLoader::getInstance($this->model);
			$xmlDataLoader->setModel($this->model);
			$xmlDataLoader->load();
		}
		__debug(__FILE__,__LINE__,"Application::parseQuery() - end");
		return false;
	}
	/**
	 * action from HTTP query anaylisis need data retrirving.
	 * @return nothin.
	 */	
	protected function retrieveData(){
		__debug(__FILE__,__LINE__,"Application::retrieveData() - start");
		__debug(__FILE__,__LINE__,"Application::retrieveData() - end");
			
	}
	/**
	 * Main method to generate page.
	 * @return nothing.
	 */
	public function run(){
		$this->startTime=microTime();
		__debug(__FILE__,__LINE__,"Application::run() - start");
		if($this->parseQuery()){
			$this->retrieveData();
		}
		$this->themes->display('index');
		$this->themes->render();
		__debug(__FILE__,__LINE__,"Application::run() - end");
		$this->endTime=microTime();
		Debug::getInstance()->flush($this->endTime - $this->startTime);
		//echo "time generation:".($this->endTime - $this->startTime)." !";
	}
	
	/**
	 * Return the Persistence Manager.
	 * @return unknown_type
	 */
	public function getPersistence(){
		return $this->persistence;
	}
	/**
	 * return version number of this application framekwork.
	 * @return string
	 */	
	public function getVersion(){
		return $this->version;
	}
	
	/**
	 * return the static instance of the THeme Manager.
	 * create the instance if does not already exist.
	 */
	public function getInstance(){
		if(!isset(self::$_instance)){
			self::$_instance = new Application();
		}
		return self::$_instance;
	}
	
}
/**
 * Accelerator to Applicatio instance.
 * @return Application Singleton Instance.
 */
function __APP(){
	$app = Application::getInstance();
	return $app;
}
/**
 * Provide persistence manager.
 * @param string $entity name of entity to manage.
 * @return Entity instance for the $entity.
 */
function __ENTITYMGR($entity){
	return __APP()->getPersistence()->getManager($entity);
}
?>